package cn.itcast.consumer.web;

import cn.itcast.consumer.pojo.Order;
import cn.itcast.consumer.pojo.Result;
import cn.itcast.user.client.UserClient;
import cn.itcast.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("order")
public class OrderController {

    /*@Autowired
    private RestTemplate restTemplate;*/

    @Autowired
    private UserClient userClient;

    @GetMapping("{userId}")
    public Result queryOrderByUserId(@PathVariable("userId") Long userId) {
        // 1.查询用户
        User user = userClient.queryUserById(userId);
        // 3.根据userId查询订单信息，并封装用户信息到订单中
        Order order = queryOrderByUser(user);
        // 4.返回订单信息到前端
        return Result.ok(order);
    }

    /*@HystrixCommand(fallbackMethod = "queryOrderFallback")
    @GetMapping("{userId}")
    public Result queryOrderByUserId(@PathVariable("userId") Long userId) {
        if (userId == 1) {
            throw new RuntimeException("我是故意的！");
        }
        // 1.准备请求的url地址
        String url = "http://user-service/user/" + userId;
        // 2.根据userId去user-service查询用户信息
        User user = restTemplate.getForObject(url, User.class);
        // 3.根据userId查询订单信息，并封装用户信息到订单中
        Order order = queryOrderByUser(user);
        return Result.ok(order);
    }

    // 查询业务的降级逻辑
    public Result queryOrderFallback(Long userId){
        return Result.error("服务器太忙了，请稍后再试！");
    }*/


    /*@Autowired
    private DiscoveryClient discoveryClient;

    // volatile int index = 0;
    @GetMapping("{userId}")
    public Order queryOrderByUserId(@PathVariable("userId") Long userId) {
        // 根据服务名称，拉取服务实例列表
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        // 从服务列表中挑一个（负载均衡）
        int index = new Random().nextInt(instances.size());
        // index = (index + 1 ) % instances.size();
        ServiceInstance instance = instances.get(index);
        // 获取实例的ip和端口
        String host = instance.getHost();
        int port = instance.getPort();

        // 1.准备请求的url地址
        String url = String.format("http://%s:%d/user/%d", host, port, userId);;
        // 2.根据userId去user-service查询用户信息
        User user = restTemplate.getForObject(url, User.class);
        // 3.根据userId查询订单信息，并封装用户信息到订单中
        Order order = queryOrderByUser(user);
        // 4.返回订单信息到前端
        return order;
    }*/
    /*@GetMapping("{userId}")
    public Order queryOrderByUserId(@PathVariable("userId") Long userId) {
        // 1.准备请求的url地址
        String url = "http://localhost:8081/user/" + userId;
        // 2.根据userId去user-service查询用户信息
        User user = restTemplate.getForObject(url, User.class);
        // 3.根据userId查询订单信息，并封装用户信息到订单中
        Order order = queryOrderByUser(user);
        // 4.返回订单信息到前端
        return order;
    }*/


    private Order queryOrderByUser(User user) {
        return new Order(110L, user.getId(), user.getName(), 2999L);
    }
}
